package List.map;
import java.util.*;
/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/5
 */
public class BookManager {
    public static void main(String[] args) {
        Map<String, ArrayList<Book>> map = new HashMap<>();
        ArrayList<Book> it = new ArrayList<Book>();
        it.add(new Book("Java入门到精通", 99));
        it.add(new Book("PHP入门到精通", 9.9));
        map.put("it书籍", it);
        ArrayList<Book> mz = new ArrayList<Book>();
        mz.add(new Book("西游记", 19));
        mz.add(new Book("水浒传", 29));
        map.put("名著", mz);
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.查看所有书籍");
            System.out.println("2.添加书");
            System.out.println("3.删除书");
            System.out.println("4.修改书");
            System.out.println("5.退出");
            System.out.println("请输入你的选择：");
            Scanner scanner = new Scanner(System.in);
            int num=scanner.nextInt();
            switch (num) {
                case 1:
                    // 查看所有书籍
                    findAllBook(map);
                    break;
                case 2:
                    // 添加书籍
                    addBook(map);
                    break;
                case 3:
                    // 删除书
                    deleteBook(map);
                    break;
                case 4:
                    // 修改书
                    editBook(map);
                    break;
                case 5:
                    // 退出
                    System.out.println("谢谢你的使用!");
                    System.exit(0);
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }
        }
    }
    private static void findAllBook(Map<String, ArrayList<Book>> map) {
        System.out.println("类型\t\t书名\t\t价格");
        Set<Map.Entry<String, ArrayList<Book>>> entries = map.entrySet();
        for (Map.Entry<String, ArrayList<Book>> entry : entries) {
            String key = entry.getKey();
            System.out.println(key);
            ArrayList<Book> value = entry.getValue();
            for (Book book : value) {
                System.out.println("\t\t" + book.getName() + "\t" + book.getPrice());
            }
        }
    }
    private static void addBook(Map<String, ArrayList<Book>> map) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要添加书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要添加的书名:");
        String name = sc.next();
        System.out.println("请输入要添加书的价格:");
        double price = sc.nextDouble();
        Book book = new Book(name, price);
        //通过键：书籍类型找值：具体书的名称和价格
        ArrayList<Book> books = map.get(type);
        if (books == null) {
            books = new ArrayList<>();
            map.put(type, books);
        }
        books.add(book);
        System.out.println("添加" + name + "成功");
    }
    private static void deleteBook(Map<String, ArrayList<Book>> map) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要删除的书名:");
        String name = sc.next();
        ArrayList<Book> books = map.get(type);
        if (books == null) {
            System.out.println("您删除的书籍类型不存在");
            return;
        }

        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            if (book.getName().equals(name)) {
                books.remove(i);
                System.out.println("删除" + name + "书籍成功");
                return;
            }
        }
        System.out.println("没有找到" + name + "书籍");
    }
    private static void editBook(Map<String, ArrayList<Book>> map) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要修改的书名:");
        String oldName = sc.next();

        System.out.println("请输入新的书名:");
        String newName = sc.next();
        System.out.println("请输入新的价格:");
        double price = sc.nextDouble();

        ArrayList<Book> books = map.get(type);
        if (books == null) {
            System.out.println("您修改的书籍类型不存在");
            return;
        }

        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            if (book.getName().equals(oldName)) {
                book.setName(newName);
                book.setPrice(price);
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("没有找到" + oldName + "书籍");
    }
}


